
import cv2
import imutils

def skip_frame():
    cap = cv2.VideoCapture(VIDEO_PATH)
    fps = int(cap.get(5))
    t = int(1000/fps)# 毫秒
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))  
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))  
    print("Camera width: ", width)  
    print("Camera height: ", height) 
    print('fps:', fps)
    videoWriter =None
    while True:
        now_fps = cap.get(1)
        # 设置每 10 帧输出一次
        if (now_fps % 10 != 0):
            # 跳帧
            ret = cap.grab()
            if not ret:
                break
            continue
        print("———————————————————— read fps", now_fps)
        _, frame = cap.read()
        if frame is None:
            break
        result = imutils.resize(frame, height=500)
        if videoWriter is None:
            fourcc = cv2.VideoWriter_fourcc(
                'm', 'p', '4', 'v')  # opencv3.0
            videoWriter = cv2.VideoWriter(
                RESULT_PATH, fourcc, fps, (result.shape[1], result.shape[0]))
        videoWriter.write(result)
        cv2.imshow("name", result)
        cv2.waitKey(t)
        if cv2.getWindowProperty("name", cv2.WND_PROP_AUTOSIZE) < 1:
            # 点x退出
            break
    cap.release()
    videoWriter.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    VIDEO_PATH = r"origin_video.mp4"
    RESULT_PATH = r"skip_video.mp4"
    skip_frame()